package web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import dao.CommentDAO;
import dao.UserDAO;
import beans.Comment;

public class CommentsController extends MultiActionController {

	private UserDAO userDAO;
	private CommentDAO commentDAO;

	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}
	
	public CommentDAO getCommentDAO() {
		return commentDAO;
	}



	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@RequestMapping(value = "/comments.htm")
	public ModelAndView comments(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();

		
		modelMap.addAttribute("user", userDAO.getLoggedUser());
		modelMap.addAttribute("commentList", commentDAO.DisplayComments());
		return new ModelAndView("comments", modelMap);
	}
	
	

	@RequestMapping(value="/deletecomment.htm")
	   public ModelAndView deletecomment (HttpServletRequest request,
            HttpServletResponse response) throws Exception{
	          int id = Integer.parseInt(ServletRequestUtils.getRequiredStringParameter(request, "comment_id"));
	          
	          Comment comment = commentDAO.GetCommentById(id);
	          commentDAO.deleteComment(comment);
	  		   
	  		   return new ModelAndView("redirect:comments.htm");
	          
		}
}
